﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using Koeltp.DataAccess;
public partial class admin_login : System.Web.UI.Page
{
    IDataAccess da = IDataAccessFactory.CreateDataAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        //指示该页上包含的所有验证控件验证指派给它们的信息
        //执行页面上验证控件的验证
        Page.Validate();
        if (!Page.IsValid)//验证失败直接返回。
            return;
        //if (true)
        if (ValidUserInfo(tbName.Text.Trim(), tbPassword.Text.Trim()))
        {
            FormsAuthentication.RedirectFromLoginPage(tbName.Text.Trim(), false);
        }
        //else
        //{
        //    lbPrompt.Visible = true;
        //    lbPrompt.Text = "<font color=\"red\">&nbsp;&nbsp;对不起！用户名或者密码错误！请重新输入。</font>";
        //    tbName.Focus();
        //}
        if (!Page.IsPostBack)
        {
            Response.Cookies.Add(new HttpCookie("CheckCode", ""));
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (Request.Cookies["CheckCode"] == null)
        {
            lbPrompt.Text = "您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
            lbPrompt.Visible = true;
            return;
        }
        if (string.Compare(Request.Cookies["CheckCode"].Value, tbVerifyCode.Text.Trim(), true) != 0)
        {
            lbPrompt.Text = "<font color=\"red\">&nbsp;&nbsp;对不起，验证码错误！</font>";
            lbPrompt.Visible = true;
        }
    }

    private bool ValidUserInfo(string userName, string userPassword)
    {
        string sql = "select Users_Id,Users_Name,Users_Password from T_Users where Users_Name=@Users_Name and Users_Password=@userPassword";
        QueryParameter[] paras = new QueryParameter[2];
        paras[0] = new QueryParameter("@Users_Name", userName, DbType.String);
        paras[1] = new QueryParameter("@userPassword", userPassword, DbType.String);
        DataTable dt = new DataTable();
        da.GetTable(sql, dt, CommandType.Text, paras);
        if (dt.Rows.Count != 0)
        {
            Session["Users_Id"] = dt.Rows[0]["Users_Id"].ToString();
            return true;
        }
        else
        {
            Session["Users_Id"] = "1";
            return false;
        }
        
    }
}